package wechat.base.snd;

import java.io.File;
import org.apache.log4j.Logger;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.eclipse.xtext.xbase.lib.Pure;
import org.json.JSONObject;
import wechat.base.snd.QRCode;
import wechat.base.snd.WCServiceApp;
import wechat.base.snd.util.ExecGet;
import wechat.base.snd.util.ExecPost;

@SuppressWarnings("all")
public class QRCodeManager {
  private static String WECHAT_QRCODE_URL = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=";
  
  private static Logger logger = Logger.getLogger("QRCodeManager");
  
  private QRCode qrcode;
  
  @Accessors
  private WCServiceApp app;
  
  public QRCodeManager(final WCServiceApp a) {
    this.app = a;
  }
  
  public String getQRCodeUrl() {
    String _token = this.app.getToken();
    return (QRCodeManager.WECHAT_QRCODE_URL + _token);
  }
  
  public QRCodeManager genPermTicket(final QRCode _qrcode) {
    QRCodeManager _xblockexpression = null;
    {
      this.qrcode = _qrcode;
      String _qRCodeUrl = this.getQRCodeUrl();
      QRCodeManager.logger.info(_qRCodeUrl);
      JSONObject _permJsonRequst = this.qrcode.toPermJsonRequst();
      String _string = _permJsonRequst.toString();
      QRCodeManager.logger.info(_string);
      String _qRCodeUrl_1 = this.getQRCodeUrl();
      JSONObject _permJsonRequst_1 = this.qrcode.toPermJsonRequst();
      String _string_1 = _permJsonRequst_1.toString();
      String _excuteJsonPost = ExecPost.excuteJsonPost(_qRCodeUrl_1, _string_1);
      final Procedure1<String> _function = new Procedure1<String>() {
        public void apply(final String it) {
          try {
            JSONObject _jSONObject = new JSONObject(it);
            final Procedure1<JSONObject> _function = new Procedure1<JSONObject>() {
              public void apply(final JSONObject it) {
                try {
                  String _string = it.toString();
                  QRCodeManager.logger.info(_string);
                  String _string_1 = it.getString("ticket");
                  QRCodeManager.this.qrcode.setTicket(_string_1);
                } catch (Throwable _e) {
                  throw Exceptions.sneakyThrow(_e);
                }
              }
            };
            ObjectExtensions.<JSONObject>operator_doubleArrow(_jSONObject, _function);
          } catch (Throwable _e) {
            throw Exceptions.sneakyThrow(_e);
          }
        }
      };
      ObjectExtensions.<String>operator_doubleArrow(_excuteJsonPost, _function);
      _xblockexpression = this;
    }
    return _xblockexpression;
  }
  
  public QRCodeManager genTempTicket(final QRCode _qrcode) {
    QRCodeManager _xblockexpression = null;
    {
      this.qrcode = _qrcode;
      String _qRCodeUrl = this.getQRCodeUrl();
      QRCodeManager.logger.info(_qRCodeUrl);
      JSONObject _tempJsonRequst = this.qrcode.toTempJsonRequst();
      String _string = _tempJsonRequst.toString();
      QRCodeManager.logger.info(_string);
      String _qRCodeUrl_1 = this.getQRCodeUrl();
      JSONObject _tempJsonRequst_1 = this.qrcode.toTempJsonRequst();
      String _string_1 = _tempJsonRequst_1.toString();
      String _excuteJsonPost = ExecPost.excuteJsonPost(_qRCodeUrl_1, _string_1);
      final Procedure1<String> _function = new Procedure1<String>() {
        public void apply(final String it) {
          try {
            JSONObject _jSONObject = new JSONObject(it);
            final Procedure1<JSONObject> _function = new Procedure1<JSONObject>() {
              public void apply(final JSONObject it) {
                try {
                  String _string = it.toString();
                  QRCodeManager.logger.info(_string);
                  String _string_1 = it.getString("ticket");
                  QRCodeManager.this.qrcode.setTicket(_string_1);
                } catch (Throwable _e) {
                  throw Exceptions.sneakyThrow(_e);
                }
              }
            };
            ObjectExtensions.<JSONObject>operator_doubleArrow(_jSONObject, _function);
          } catch (Throwable _e) {
            throw Exceptions.sneakyThrow(_e);
          }
        }
      };
      ObjectExtensions.<String>operator_doubleArrow(_excuteJsonPost, _function);
      _xblockexpression = this;
    }
    return _xblockexpression;
  }
  
  public File getTicket(final String filename_prefix) {
    try {
      String _ticket = this.qrcode.getTicket();
      String _plus = ("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + _ticket);
      return ExecGet.excuteGetImg(_plus, (filename_prefix + ".jpg"));
    } catch (Throwable _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  @Pure
  public WCServiceApp getApp() {
    return this.app;
  }
  
  public void setApp(final WCServiceApp app) {
    this.app = app;
  }
}
